<?php
global $_W, $_GPC;
load()->model('mc');
$config = $this->module['config'];
$pageTitle = $_W['account']['name'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$pindex = max(1, intval($_GPC['page']));
$psize  = 10;

$xcx = intval($_GPC["xcx"]);
$unionid = $_GPC["unionid"];
if($xcx == 1){
	$uniacid    = intval($_W['uniacid']);
	$user_u = pdo_get("mc_mapping_fans", array("unionid" => $unionid, "uniacid" => $uniacid));
	$openid     = $user_u['openid'];
	$follow = followed($openid);
	if($follow == 0){// && intval($config['isfollow']) == 1 && intval($config['followtype']) == 0
		
		echo $config['followlink'];
		exit();
		header("location:".$config['followlink']);
	}
}else{
	$uniacid    = intval($_W['uniacid']);
	$fans       = $_W['fans'];
    $openid     = $fans['openid'];
}

$isDebug = false;

if($isDebug){
    $openid = 'fromUser';
}else{
    if(!empty($_GPC["openid"])){
        $openid = $_GPC["openid"];
    }
    if(empty($openid)){
        checkauth();
    }
    if(empty($openid)){ 
        show_json(0,"不允许外部访问！");
    }
}


$member = checkMember($openid);
$member['avatar'] = tomedia($member['avatar']);

$uid   = intval(mc_openid2uid($openid));

if(empty($uid)){
    $uid = $member['uid'];
}

# 记录会员登录信息
$members = pdo_fetch("SELECT id,nickname FROM ".tablename(MODULE_PREFIX."member")." WHERE uniacid = $uniacid and uid = $uid ");
if(empty($members)){
    $members = array(
        'uniacid' => $uniacid,
        'uid' => $uid,
        'openid' => $openid,
        'nickname' => strval($member['nickname']),
        'scancodenum' => 0,
        'createtime' => TIMESTAMP
    );

     // 最后一个排名
    $last_ranking = pdo_fetchcolumn("select ranking from ".tablename(MODULE_PREFIX."member")." where uniacid =:uniacid order by id desc limit 1 ",array(':uniacid'=>$_W['uniacid']));

    pdo_insert(MODULE_PREFIX."member",$members);
    $last_id = pdo_insertid();

    $ranking = intval($last_ranking) + 1;
    pdo_update(MODULE_PREFIX."member",array('ranking'=>$ranking),array('id'=>$last_id));
    $members['ranking'] = $ranking;
}else{
    if(empty($members['nickname']) && !empty($member['nickname'])){
        pdo_update(MODULE_PREFIX."member",array('nickname'=>$member['nickname']),array('id'=>$members['id']));
    }
}

$_share = array(
    'title'   => $config['share_title'],
    'link'    => $config['share_link'],
    'imgUrl'  => tomedia($config['share_imgurl']),
    'content' => $config['share_desc']
);

$path = $_W['siteroot'] . "addons/".MODULE_NAME."/static/data/" . $uniacid . "/";
